#!/bin/bash
# 卸载脚本
# author    : guyadong
# date      : 2019/08/19

sh_folder=$(dirname $(readlink -f $0))

# 脚本初始化
. ./common_init.sh

# 获取发行版本(ubuntu,centos...)
[ -f /etc/os-release ] || exit_on_error "ERROR:NOT FOUND /etc/os-release"
eval dist_id=$(get_value /etc/os-release ID)
. ./native_functions_${dist_id,,}.sh || exit_on_error "不支持当前平台:$dist_id"

# 关闭防火墙端口
function close_port_firewall(){
     echo "防火墙设置(firewall setup)"
    [ "$facelog_install" = 'on'  ] && firewall_close_port $facelog_port
    [ "$facelog_install" = 'on'  ] && firewall_close_port $DEFAULT_FACELOG_PORT_XHR
    [ "$facelog_install" = 'on'  ] && firewall_close_port $DEFAULT_FACELOG_PORT_RESTFUL
    [ "$webredis_install" = 'on' ] && firewall_close_port $webredis_port
    [ "$fdevmgr_install" = 'on'  ] && firewall_close_port $fdevmgr_port
    [ "$fdevmgr_install" = 'on'  ] && firewall_close_port $nginx_port
    [ $redis_use_local -eq 0     ] && firewall_close_port $redis_port
    firewall_reload
    echo "==========================="
    return 0
}

# 卸载所有安装的第三方软件包
function uninstall_dependencies(){
    echo "卸载所有安装的第三方软件包"
    [ -n "$installed_deps" ] && uninstall_components "${installed_deps//,/ }"
    set_status_value 'installed_deps' ''
    uninstall_dependencies_ext
    echo "==========================="
    return 0
}

# 删除 nginx 配置
function remove_fdevmgrweb_nginx_conf(){
    # 恢复 nginx 默认站点
    if [ ! -f /etc/nginx/sites-enabled/default ] && [ -f /etc/nginx/sites-available/default ]
    then 
        sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default || exit_on_error
    fi
    # 删除 fdevmgrweb nginx配置
    sudo rm -f /etc/nginx/sites-enabled/$FDEVMGRWEB_NGINX_CONF || exit_on_error
    sudo rm -f /etc/nginx/conf.d/$FDEVMGRWEB_NGINX_CONF || exit_on_error

    restore_facelog_bak /etc/nginx/nginx.conf
    restore_facelog_bak /etc/nginx/conf.d/default.conf
    sudo systemctl restart nginx
}

# 删除 redis 
function remove_redis(){    
    systemctl_remove redis-server-$redis_port
    sudo rm -f /etc/redis/redis-$redis_port.conf || exit_on_error
    
    sudo rm -f /var/run/redis/redis-server-$redis_port.pid || exit_on_error
    sudo rm -f /var/log/redis/redis-server-$redis_port.log || exit_on_error
    sudo rm -f /var/lib/redis/dump-$redis_port.db || exit_on_error
    
}

# 删除数据库
function remove_database(){
    [ "$facelog_install" = 'on' ] && mysql_remove_database \
                    $database_jdbc_host \
                    $database_jdbc_port \
                    $database_jdbc_username \
                    $database_jdbc_password \
                    $database_jdbc_schema \
                    'facelog'


    [ "$fdevmgr_install" = 'on' ] && mysql_remove_database \
                    $fdevmgr_db_host \
                    $fdevmgr_db_port \
                    $fdevmgr_db_username \
                    $fdevmgr_db_password \
                    $fdevmgr_db_schema \
                    'fdevmgr'

}

# 删除 facelog 配置文件
function remove_config(){
    echo "删除 facelog 配置文件"
    rm -frv $HOME/$CFG_FOLDER
    echo "==========================="
    return 0
}

# 删除所有安装到系统的服务及对应的配置文件
function remove_service(){

    remove_config

    remove_fdevmgrweb_nginx_conf

    systemctl_remove 'fdevmgr'
    systemctl_remove 'webredis'
    systemctl_remove 'facelog'
    systemctl_remove 'nginx'

    remove_redis
    
    sudo systemctl reset-failed    
}
# 显示命令行参数说明
usage() {
    echo "facelog 卸载脚本"
    echo "不带任何参数时默认删除安装的服务以及应用数据,保留数据库及相关的依赖软件"
    echo "用法:"
    echo "  uninstall.sh [OPTION]..."
    echo "参数说明:"
    echo "    -d --database 卸载所有安装的服务以及删除数据库"
    echo "    -a --all 卸载所有安装的服务及数据库以及相关的依赖软件(mysql,redis...)"
    echo "    -h --help 输出帮助信息"
    exit 255
}

remove_all=1
remove_db=1
# 解析命令行参数
while [ $# -ge 1 ] ; do
    case "$1" in
        -h|--help) usage;;
        -a|--all)
            remove_all=0;
            shift 1;;
        -d|--database)
            remove_db=0;
            shift 1;;
        *) 
            echo "无效参数 $1."
            usage
            shift 1;;
    esac
done

remove_service
if [ $remove_all -eq 0 ] || [ $remove_db -eq 0 ]
then
    remove_database
fi
close_port_firewall
[ $remove_all -eq 0 ] && uninstall_dependencies
